//
//  TPPhotoBrowserViewController.m
//  美丽说图片浏览器
//
//  Created by 唐鹏 on 16/7/13.
//  Copyright © 2016年 tangpeng. All rights reserved.
//

#import "TPPhotoBrowserViewController.h"
#import "TPPhotoBrowserFlowLayout.h"
#import "TPPhotoBrowserViewCell.h"
#import "TPHomeCellItem.h"
#import "TPPhotoBrowserAnimation.h"

@interface TPPhotoBrowserViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

/***/
@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation TPPhotoBrowserViewController

//- (TPPhotoBrowserAnimation *)animation{
//    if (!_animation) {
//        _animation = [[TPPhotoBrowserAnimation alloc] init];
//    }
//    return  _animation;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.view.width += 15;
    
    [self createCollectionView];
    [self.collectionView registerClass:[TPPhotoBrowserViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self setupBtn];
    
    [self.collectionView scrollToItemAtIndexPath:self.indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)createCollectionView{
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[[TPPhotoBrowserFlowLayout alloc] init]];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
}

- (void)setupBtn{
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self createBtn:closeBtn action:@selector(close) title:@"关闭"];
    [self createBtn:saveBtn action:@selector(save) title:@"保存"];
    closeBtn.frame = CGRectMake(20, tp_screenHeight - 20 - 32, 90, 32);
    saveBtn.frame = CGRectMake(tp_screenWidth - 90 - 20, tp_screenHeight - 20 - 32, 90, 32);
    [self.view addSubview:closeBtn];
    [self.view addSubview:saveBtn];
}

- (void)close{
    self.imageV = [self getImageView];
    self.path = [self getIndexPath];
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)save{
    
}

- (void)createBtn:(UIButton *)button action:(SEL)action title:(NSString *)title{
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:[UIColor darkGrayColor]];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.shops.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TPPhotoBrowserViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.item = self.shops[indexPath.row];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self close];
}

- (UIImageView *)getImageView{
    
    NSIndexPath *path = [self getIndexPath];
    TPPhotoBrowserViewCell *cell = (TPPhotoBrowserViewCell *)[self.collectionView cellForItemAtIndexPath:path];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = cell.imageView.image;
    imageView.frame = cell.imageView.frame;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
    return imageView;
}

- (NSIndexPath *)getIndexPath{
    
    TPPhotoBrowserViewCell *cell = self.collectionView.visibleCells.firstObject;
    return [self.collectionView indexPathForCell:cell];
}





@end
